package com.eugene.scheduler;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.ToggleButton;

import com.eugene.scheduler.SchedulerWidget;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Eugene on 25.12.13.
 */
public class CalendarSettings extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings);

        List<String> SpinnerArray =  new ArrayList<String>();
        SpinnerArray.add("1 x 1");
        SpinnerArray.add("2 x 2");
        SpinnerArray.add("3 x 3");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner changes = (Spinner) findViewById(R.id.spinner_changes);
        changes.setAdapter(adapter);
        changes.setSelection(SchedulerWidget.workDays - 1);

        DatePicker picker = (DatePicker) findViewById(R.id.datePicker);
        picker.init(SchedulerWidget.beginDate.get(Calendar.YEAR), SchedulerWidget.beginDate.get(Calendar.MONTH),
                SchedulerWidget.beginDate.get(Calendar.DAY_OF_MONTH), null);

        Switch showCh = (Switch) findViewById(R.id.switch_changes);
        showCh.setChecked(SchedulerWidget.showChanges);

        Switch showUser = (Switch) findViewById(R.id.switch_user);
        showUser.setChecked(SchedulerWidget.showUserChanges);

        Switch showHol = (Switch) findViewById(R.id.switch_holidays);
        showHol.setChecked(SchedulerWidget.show2014Holidays);

        List<String> themeList =  new ArrayList<String>();
        for(int i=ColorProvider.Style.Glass.getValue(); i<=ColorProvider.Style.Notebook.getValue(); i++){
            themeList.add(ColorProvider.getStyleName(i));
        }
        Spinner themes = (Spinner) findViewById(R.id.theme_spinner);
        ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, themeList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        themes.setAdapter(list);
        themes.setSelection(SchedulerWidget.theme);

        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Switch showCh = (Switch) findViewById(R.id.switch_changes);
                SchedulerWidget.showChanges = showCh.isChecked();
                Switch showUser = (Switch) findViewById(R.id.switch_user);
                SchedulerWidget.showUserChanges = showUser.isChecked();
                Switch showHol = (Switch) findViewById(R.id.switch_holidays);
                SchedulerWidget.show2014Holidays = showHol.isChecked();

                DatePicker pick = (DatePicker) findViewById(R.id.datePicker);
                int day = pick.getDayOfMonth();
                int month = pick.getMonth();
                int year =  pick.getYear();
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, month, day);
                SchedulerWidget.beginDate = calendar;

                Spinner spin = (Spinner) findViewById(R.id.spinner_changes);
                SchedulerWidget.workDays = spin.getSelectedItemPosition() + 1;
                SchedulerWidget.restDays = spin.getSelectedItemPosition() + 1;

                Spinner themes = (Spinner) findViewById(R.id.theme_spinner);
                SchedulerWidget.theme = themes.getSelectedItemPosition();

                switch(themes.getSelectedItemPosition()){
                    case 0: ColorProvider.style = ColorProvider.Style.Glass; break;
                    case 1: ColorProvider.style = ColorProvider.Style.OnePiece; break;
                    case 2: ColorProvider.style = ColorProvider.Style.Green; break;
                    case 3: ColorProvider.style = ColorProvider.Style.Transparent; break;
                    case 4: ColorProvider.style = ColorProvider.Style.Notebook; break;
                    default: ColorProvider.style = ColorProvider.Style.Notebook; break;
                }

                updateParent();

                finish();
            }
        });
    }

    void updateParent(){
        Intent intent = new Intent(this, SchedulerWidget.class);
        intent.setAction("com.example.android.scheduler.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
        int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), SchedulerWidget.class));
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
        sendBroadcast(intent);
    }
}